package com.my_musc_service.common.timedtask.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.my_musc_service.dao.SharedCommentDao;
import com.my_musc_service.dao.SharedSongDao;
import com.my_musc_service.dao.SharedSongListDao;
import com.my_musc_service.entity.SharedSong;
import com.my_musc_service.entity.SharedSongList;
import com.my_musc_service.entity.SharedSongListStatistics;
import com.my_musc_service.entity.SharedSongStatistics;
import com.my_musc_service.utils.spring.SpringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.List;

/**
 * @author Zhu Dunfeng
 * @date 2021/8/29 20:39
 */
public class StatisticCommentJob implements Job {


    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        SharedCommentDao sharedCommentDao = SpringUtils.getBean(SharedCommentDao.class);
        SharedSongDao sharedSongDao = SpringUtils.getBean(SharedSongDao.class);
        SharedSongListDao sharedSongListDao = SpringUtils.getBean(SharedSongListDao.class);
        //统计分享歌曲的评论数
        List<SharedSongStatistics> sharedSongStatisticsList = sharedCommentDao.selectSharedSongCommentCount();
        for (SharedSongStatistics sharedSongStatistics : sharedSongStatisticsList) {
            SharedSong sharedSong = sharedSongDao
                    .selectOne(new QueryWrapper<SharedSong>()
                            .eq("id", sharedSongStatistics.getSharedSongId()));
            sharedSong.setCommentCount(sharedSongStatistics.getCommentCount());
            sharedSongDao.update(sharedSong);
        }

        //统计分享歌单的评论数
        List<SharedSongListStatistics> sharedSongListStatisticsList = sharedCommentDao.selectSharedSongListCommentCount();
        for (SharedSongListStatistics sharedSongListStatistics : sharedSongListStatisticsList) {
            SharedSongList sharedSongList = sharedSongListDao.selectOne(new QueryWrapper<SharedSongList>()
                    .eq("id", sharedSongListStatistics.getSharedSongListId()));
            sharedSongList.setCommentCount(sharedSongListStatistics.getCommentCount());
            sharedSongListDao.update(sharedSongList);
        }
    }
}

//完全使用Java进行统计
//        List<SharedComment> sharedCommentList = sharedCommentDao.selectList(null);
//        List<SharedComment> sharedSongListCommentList = sharedCommentList.stream().filter(sharedComment ->
//                sharedComment.getSharedSongListId() != null )
//                .collect(Collectors.toList());
//        Map<Integer, Long> sharedSongListCommentMap = sharedSongListCommentList.stream().collect(Collectors.groupingBy(SharedComment::getSharedSongListId, Collectors.counting()));
//
//        List<SharedComment> sharedSongList = sharedCommentList.stream().filter(sharedComment ->
//                sharedComment.getSharedSongId() != null)
//                .collect(Collectors.toList());
//        Map<Integer, Long> sharedSongMap = sharedSongList.stream().collect(Collectors.groupingBy(SharedComment::getSharedSongId, Collectors.counting()));
//
//        System.out.println(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",new Date()));
//        System.out.println("sharedComments.size() = " + sharedCommentList.size());
//        System.out.println("sharedSongListCommentList.size() = " + sharedSongListCommentList.size());
//        System.out.println("sharedSongList.size() = " + sharedSongList.size());
